import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:thinking/config/Color.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/WaterFallFlow/WaterFallFlow.dart';
import 'package:thinking/widget/_.dart';
import 'package:thinking/views/Dynamic/DynamicItem.dart';

class PersonalTitleBottomWidget extends StatefulWidget {
  final int pageIndex;
  final int reqIndex;
  PersonalTitleBottomWidget({required this.pageIndex,required this.reqIndex});

  @override
  _PersonalTitleBottomWidget createState() =>
      _PersonalTitleBottomWidget(pageIndex: pageIndex,reqIndex:reqIndex);
}

class _PersonalTitleBottomWidget extends State<PersonalTitleBottomWidget> with AutomaticKeepAliveClientMixin {

  @override
  bool get wantKeepAlive => true;


  int pageIndex;
  int reqIndex;
  late EasyRefreshController _easyRefreshController;
  late PageEntity pageEntity;
  late Future<dynamic> _function;
  List<DynamicDataEntity> dynamicList = [];
  List<Think> thinkList = [];
  _PersonalTitleBottomWidget({required this.pageIndex,required this.reqIndex});

  @override
  void initState() {
    super.initState();
    _easyRefreshController = EasyRefreshController();
    pageEntity = PageEntity();
    _function = future();
  }

  future() async {
    await getData();
  }

  Future<void> getData() async {
    switch (pageIndex) {
      case 0:
        return await getThinkData();
      default:
        return await getDynamicData();
    }
  }

  Future<void> getThinkData() async {
    List<Think> res =
    (  await UserService.getPersonalThinkList(params: {
      'page':pageEntity.page,
      'count':pageEntity.count,
      'my_Id':SPreferences().getInt('user_Id'),
      'reqIndex':reqIndex
    })).thinkData!;
    if (!pageEntity.canLoad) {
      BotToast.showText(text: '没有更多think了哦~');
    }
    if (res.length < pageEntity.count && pageEntity.canLoad) {
      pageEntity.setPage = pageEntity.page +1;
      pageEntity.setCanLoad = false;
    }
    thinkList = [...thinkList,...res];
    setState(() {

    });
  }

  Future<void> getDynamicData() async {
    List<DynamicDataEntity> res =
    (  await UserService.getPersonalDynamicList(params: {
      'page':pageEntity.page,
      'count':pageEntity.count,
      'my_Id':SPreferences().getInt('user_Id'),
      'reqIndex':reqIndex
    })).dynamicList!;
    if (!pageEntity.canLoad) {
      BotToast.showText(text: '没有更多动态了哦~');
    }
    if (res.length < pageEntity.count && pageEntity.canLoad) {
      pageEntity.setPage = pageEntity.page +1;
      pageEntity.setCanLoad = false;
    }
    dynamicList = [...dynamicList,...res];
    setState(() {

    });
  }

  Widget easyRefresh(Widget child) {
    return EasyRefresh(
      child: child,
      header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
      footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      bottomBouncing: true,
      onLoad: () async {
        if (pageEntity.canLoad) {
          pageEntity.setPage = pageEntity.page + 1;
        }
        await getData();
        _easyRefreshController.finishLoad(success: true);
      },
    );
  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
      future: _function,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        return snapshot.connectionState == ConnectionState.done
            ? easyRefresh(Empty(

                isShow: pageIndex==0?thinkList.length==0:dynamicList.length==0,
                child: Container(
                  margin: EdgeInsets.only(top: pageIndex==0?10:5),
                  child: ListView(
                      physics: BouncingScrollPhysics(),
                      shrinkWrap: true,
                      children: pageIndex == 0
                      ? [SpannableCountCountPage(thinkDataList: thinkList)]
                          : dynamicList.asMap().keys
                      .map((e) => Column(
                    children: [
                    Visibility(child: Line(),visible:e!=0),
          DynamicItem(
            dynamicData: dynamicList[e],
            key: ValueKey(dynamicList[e].dynamicId),
          )
          ],
        ))
            .toList(),
        ),
                )))
            : Loading();
      },
    );
  }
}
